import pandas as pd

from pylab import *

def main():
    s = pd.Series([i*2 for i in range(1, 11)])
    print(type(s))

    dates = pd.date_range("20170301", periods=8)
    df = pd.DataFrame(np.random.randn(8, 5), index=dates, columns=list('ABCDE'))
    print(df)

    print(df.head(3))
    print(df.tail(3))
    print(df.index)
    print(df.values)
    print(df.T)
    print(df.sort_index(axis=1, ascending=False))
    print(df.describe())
    print(df["A"])
    print(type(df["A"]))
    print(df[:3])
    print(df.at[dates[0], "C"])

    s1 = pd.Series(list(range(10, 18)), index=pd.date_range("20170301", periods=8))
    df["F"] = s1
    print(df)
    df.at[dates[0], "A"] = 0
    df.iat[1, 1] = 1
    df.loc[:, 'D'] = np.array([4]*len(df))
    print(df)
    df2 = df.copy()
    df2[df2 > 0] = -df2
    print(df2)


    df1 = df.reindex(index=dates[:4],columns=list("ABCD")+["G"])
    df1.loc[dates[0]:dates[1], "G"] = 1
    print(df1)
    print(df1.dropna())
    print(df1.fillna(value=1))
    print(df1.fillna(value=2))


    print(df.mean())
    print(df.var())
    s = pd.Series([1,2,4,np.nan,5,7,9,10],index=dates)
    print(s)
    print(s.shift(2))
    print(s.diff())
    print(s.value_counts())
    print(df.apply(np.cumsum))
    print(df.apply(lambda x:x.max()-x.min()))

    df4 = pd.DataFrame({'A':['one','one','two','three']*6,
                       'B':['a','b','c']*8,
                        'C':['foo','foo','foo','bar','bar','bar']*4,
                        'D':np.random.randn(24),
                        'E':np.random.randn(24),
                        'F':[datetime.datetime(2017,i,1) for i in range(1, 13)] +
                            [datetime.datetime(2017,i,15) for i in range(1, 13)]
                        })
    print(pd.pivot_table(df4,values="D",index=["A","B"],columns=["C"]))
    t_exam = pd.date_range("20170301",periods=10, freq="S")
    print(t_exam)

    ts = pd.Series(np.random.randn(1000),index=pd.date_range("20170301",periods=1000))
    ts = ts.cumsum()
    ts.plot()
    show()

    df6 = pd.read_csv("./data/test.csv")
    print("CSV",df6)
    df7 = pd.read_excel("./data/test.xlsx","Sheet1")
    print("Excel",df7)
    df6.to_csv("./data/test2.csv")
    df7.to_excel("./data/test2.xlsx")

    print(df4)
    print(t_exam)
    print(pd.pivot_table(df4, values="D", index=["A", "B"], columns=["C"]))


if __name__ =="__main__":
    main()